package org.example.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.example.entry.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("findUserAll")
    public Map<String, Object> findUserAll() {
        Map<String, Object> result = new HashMap<>();
        List<User> userList = userService.findUserAll();
        result.put("userList", userList);
        return result;
    }

    /***
     *  Excel 导出
     * @param response
     * @param request
     * @return
     */
    @RequestMapping("exportExcel")
    public String exportExcel(HttpServletResponse response, HttpServletRequest request) {
        List<User> userAll = userService.findUserAll();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户列表", "用户信息"), User.class, userAll);
        try {
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("用户列表.xls", "UTF-8"));
            ServletOutputStream os = response.getOutputStream();
            workbook.write(os);
            os.close();
            workbook.close();
            return "SUCCESS";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "ERROR :" + "msg: " + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "ERROR :" + "msg: " + e.getMessage();
        }
    }

    /***
     * 导入excel
     */
    @RequestMapping("import")
    public String importExcel(@Param("file") MultipartFile file) {
        log.info("文件名称: [{}]", file.getOriginalFilename());
        ImportParams params = new ImportParams();
        params.setTitleRows(1);//设置一级标题行为1行
        params.setHeadRows(1);//设置header标题为1行
        try {
            List<User> userList = ExcelImportUtil.importExcel(file.getInputStream(), User.class, params);
            log.info("导入总数为: [{}]", userList.size());
            userList.forEach(System.out::println);
            return "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            return "error = msg: " + e.getMessage();
        }
    }


}
